package com.fkdx.common.sys;

import com.fkdx.common.enums.sys.ErrCodeEnum;
import com.fkdx.common.enums.sys.GroupException;
import com.fkdx.common.dto.sys.CurrentUserDTO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityHelper {

    public static CurrentUserDTO getPrincipal() {
        Authentication authentication = getAuthentication();
        if (authentication != null) {
            Object details = authentication.getDetails();
            if (details != null) {
                return (CurrentUserDTO) details;
            }
        }
        throw new GroupException(ErrCodeEnum.SESSION_EXPIRED.getErrMsg());
    }

    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

}
